#include <bits/stdc++.h>
using namespace std;
struct TreeNode {
	int val;
	TreeNode *left;
	TreeNode *right;
	TreeNode() : val(0), left(nullptr), right(nullptr) {}
	TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
	TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

class Solution {
public:
	vector<int> inorderTraversal(TreeNode* root) {
		stack<TreeNode*> st;
		vector<int> result;
		st.push(root);
		while(!st.empty()){
			TreeNode* node = st.top();
			if(node != NULL){
				st.pop();
				if(node->right){
					st.push(node->right);
				}
				st.push(node);
				st.push(NULL);
				if(node->left){
					st.push(node->left);
				}
			}else{
				st.pop();
				node = st.top();st.pop();
				result.push_back(node->val);
			}
		}
		return result;
	}
};
class Solution2 {
public:
	vector<int> preorderTraversal(TreeNode* root) {
		stack<TreeNode*> st;
		vector<int> result;
		st.push(root);
		while(!st.empty()){
			TreeNode* node = st.top();
			if(node != NULL){
				st.pop();
				if(node->right){
					st.push(node->right);
				}
				if(node->left){
					st.push(node->left);
				}
				st.push(node);
				st.push(NULL);
			}else{
				st.pop();
				node=st.top();st.pop();
				result.push_back(node->val);
			}
		}
		return result;
	}
};
class Solution3 {
public:
	vector<int> postorderTraversal(TreeNode* root) {
		stack<TreeNode*> st;
		vector<int> result;
		st.push(root);
		while(!st.empty()){
			TreeNode* node = st.top();
			if(node != NULL){
				st.pop();
				st.push(node);
				st.push(NULL);
				if(node->right){
					st.push(node->right);
				}
				if(node->left){
					st.push(node->left);
				}

			}else{
				st.pop();
				node=st.top();st.pop();
				result.push_back(node->val);
			}
		}
		return result;
	}
};

class Test{
public:
	vector<int> postorderTraversal(TreeNode* root) {
		stack<TreeNode*> st;
		vector<int> result;
		st.push(root);
		while(!st.empty()){
			// 1. 先弹出栈顶
			TreeNode* root = st.top();
			if(root!= NULL){
				st.pop();
				st.push(root);
				st.push(NULL);
				if(root->left){
					st.push(root->left);
				}
				if(root->right){
					st.push(root->right);
				}
			}else{
				st.pop();
				root = st.top();
				result.push_back(root->val);
				st.pop();
			}
		}
		return result;
	}
};
